app.controller('viewContractCtrl',function($scope, user, rest, tipService, $location){
  var data = angular.copy($scope.ngDialogData, {});
  $scope.user = {
    phone: user.get('phone'),
  };
  $scope.contactRelationship = {};
  $scope.contactRelationshipList = [{
      name: '父母',
      value: '父母'
    }, {
      name: '兄弟姐妹',
      value: '兄弟姐妹'
    }, {
      name: '同事',
      value: '同事'
    }, {
      name: '同学',
      value: '同学'
    }, {
      name: '朋友',
      value: '朋友'
    }, {
      name: '亲属',
      value: '亲属'
    }, {
      name: '配偶',
      value: '配偶'
    }, {
      name: '其他',
      value: '其他'
    }]

  $scope.payType = {};
  $scope.payTypeList = [{
        name: '一月一付',
        value: 0
    }, {
        name: '二月一付',
        value: 1
    }, {
        name: '三月一付',
        value: 2
    // }, {
    //     name: '一年一付',
    //     value: 3
    }]

  $scope.payRent = {};
    $scope.payRentList = [{
        name: '0',
        value: 0
    }, {
        name: '1',
        value: 1
    }, {
        name: '2',
        value: 2
    }, {
        name: '3',
        value: 3
    }, {
        name: '4',
        value: 4
    }, {
        name: '5',
        value: 5
    }, {
        name: '6',
        value: 6
    }, {
        name: '7',
        value: 7
    }, {
        name: '8',
        value: 8
    }, {
        name: '9',
        value: 9
    }, {
        name: '10',
        value: 10
    }, {
        name: '11',
        value: 11
    }, {
        name: '12',
        value: 12
    }]

  var getAddCost = function (srcArr) {
    var result = [];
    for (var i = 0; i < srcArr.length; i++) {
      if (srcArr[i]) {
        // {AddCostType:1,Amount:500,payType:1}
        result.push({
          AddCostType: srcArr[i]._Option1.selected.value,
          payType: srcArr[i]._Option2.selected.value,
          Amount: srcArr[i]._Option3
        });
      }
    }
    return result;
  }

  if (data.contractId) {      
    rest.post({
        methodId: '4019',
        data: {id: data.contractId},
        success: function(res) {
            if (res.code == 0) {
                $scope.params = res.res.data;
                  $scope.$apply( function(){
                    $scope.params = res.res.data;     
                    $scope.payDay = $scope.params.payDay;             
                    $scope.params.idCard = $scope.params.tenantsInfo.idCard;    
                    $scope.params.noFixationEndTime = moment( $scope.params.noFixationEndTime).format('YYYY/MM/DD');
                    $scope.params.noFixationStartTime = moment( $scope.params.noFixationStartTime).format('YYYY/MM/DD');
                    $scope.params.fixationEndTime = moment( $scope.params.fixationEndTime).format('YYYY/MM/DD');
                    $scope.params.fixationStartTime = moment( $scope.params.fixationStartTime).format('YYYY/MM/DD');
                    $scope.params.leaseStartTime = moment( $scope.params.leaseStartTime).format('YYYY/MM/DD');
                    $scope.params.leaseEndTime = moment( $scope.params.leaseEndTime).format('YYYY/MM/DD');
                       // 紧急联系人下拉框  
                        $scope.contactRelationshipValue = $scope.params.tenantsInfo.contactRelationship;        
                        if ( $scope.contactRelationshipValue  + 1) {
                            $scope.contactRelationshipList.map(function(ele, index) {
                                if ($scope.contactRelationshipValue == ele.value) {
                                    $scope.contactRelationship.selected = ele;
                                }
                            })
                        }
                        
                        // 支付方式
                        $scope.payTypeValue = $scope.params.rentPayType;        
                        if ( $scope.payTypeValue  + 1) {
                            $scope.payTypeList.map(function(ele, index) {
                                if ($scope.payTypeValue == ele.value) {
                                    $scope.payType.selected = ele;
                                }
                            })
                        }

                        // 当前已交期数
                        $scope.payRentValue = $scope.params.payPeriods;        
                        if ( $scope.payRentValue  + 1) {
                            $scope.payRentList.map(function(ele, index) {
                                if ($scope.payRentValue == ele.value) {
                                    $scope.payRent.selected = ele;
                                }
                            })
                        }
                           
                } );                  
              
                $scope.$on('rentTime.start', function(event, data) {
                  $scope.params.leaseStartTime = data;
                })
                $scope.$on('rentTime.end', function(event, data) {
                  $scope.params.leaseEndTime = data;
                })
                $scope.$on('fixedTime.start', function(event, data) {
                  $scope.params.fixationStartTime = data;
                })
                $scope.$on('fixedTime.end', function(event, data) {
                  $scope.params.fixationEndTime = data;
                })
                $scope.$on('notFixedTime.start', function(event, data) {
                  $scope.params.noFixationStartTime = data;
                })
                $scope.$on('notFixedTime.end', function(event, data) {
                  $scope.params.noFixationEndTime = data;
                })
                $scope.$digest();
            }
        },
        error: function() {}
    })    
  }


  $scope.otherExp = [];

  $scope.$watch('payDay', function(newValue, oldValue) { 
        if ($scope.payDay>0 && $scope.payDay<=26 ) {
            $scope.powerOff = parseInt($scope.payDay) + 1;
            $scope.lockOff = parseInt($scope.payDay) + 2;
            $scope.mastOff = parseInt($scope.payDay) + 3;
        }else{
            $scope.powerOff = "　";
            $scope.lockOff = "　";
            $scope.mastOff = "　";
        }
  });
  
})

app.controller('TimeCtrl', function($scope) {
    $scope.today = function() {
        $scope.LeaseStartTime = moment().format('YYYY/MM/DD');
        $scope.LeaseEndTime = moment().add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD');
        $scope.FixationStartTime = moment().format('YYYY/MM/DD');
        $scope.FixationEndTime = "";
        $scope.NoFixationStartTime = "";
        $scope.NoFixationEndTime = moment().add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD');
    };

    $scope.closeAll = function() {
        $scope.openLeaseBeginDated =
            $scope.openLeaseEndDated =
            $scope.openFixationBeginDated =
            $scope.openFixationEndDated =
            $scope.openNoFixationBeginDated =
            $scope.openNoFixationEndDated = false;
    }

    $scope.openLeaseBeginDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openLeaseBeginDated = true;
    };
    $scope.openLeaseEndDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openLeaseEndDated = true;
    };

    $scope.openFixationBeginDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openFixationBeginDated = true;
    };
    $scope.openFixationEndDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openFixationEndDated = true;
    };

    $scope.openNoFixationBeginDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openNoFixationBeginDated = true;
    };
    $scope.openNoFixationEndDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.closeAll();
        $scope.openNoFixationEndDated = true;
    };

    $scope.dateOptions = {
        format: 'yyyy/MM/dd',
        startingDay: 1,
        class: 'datepicker'
    };

    $scope.format = 'yyyy/MM/dd';

    $scope.today();
    
    $scope.$watch('params.leaseStartTime', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('rentTime.start', moment(newValue).format('YYYY/MM/DD'))
            $scope.$emit('rentStartTime', moment(newValue).format('DD'))

            $scope.$emit('fixedTime.start', moment(newValue).format('YYYY/MM/DD'))
            $scope.FixationStartTime = moment(newValue).format('YYYY/MM/DD')

            $scope.$emit('rentTime.end', moment(newValue).add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD'))
            $scope.LeaseEndTime = moment(newValue).add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD')

            $scope.$emit('notFixedTime.end', moment(newValue).add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD'))
            $scope.NoFixationEndTime = moment(newValue).add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD')

            $scope.$emit('rent.date', moment(newValue).format('YYYY/MM/'))
            $scope.rentDate = moment(newValue).format('YYYY/MM/')
        }
    });
    $scope.$watch('params.leaseEndTime', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('rentTime.end', moment(newValue).format('YYYY/MM/DD'))

            $scope.$emit('notFixedTime.end', moment(newValue).format('YYYY/MM/DD'))
            $scope.NoFixationEndTime = moment(newValue).format('YYYY/MM/DD')
        }
    });
    $scope.$watch('params.fixationStartTime', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('fixedTime.start', moment(newValue).format('YYYY/MM/DD'))
        }
    });
    $scope.$watch('params.fixationEndTime', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('fixedTime.end', moment(newValue).format('YYYY/MM/DD'))

            $scope.$emit('notFixedTime.start', moment(newValue).add(1, 'days').format('YYYY/MM/DD'))
            $scope.NoFixationStartTime = moment(newValue).add(1, 'days').format('YYYY/MM/DD')
        }
    });
    $scope.$watch('params.noFixationStartTime', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('notFixedTime.start', moment(newValue).format('YYYY/MM/DD'))
        }
    });
    $scope.$watch('params.noFixationEndTime', function(newValue, oldValue) {
        if ("[object Date]" === Object.prototype.toString.call(newValue)) {
            $scope.$emit('notFixedTime.end', moment(newValue).format('YYYY/MM/DD'))
        }
    });
});